## Estimate effects of earmarks on vote share by district area in 2008 and 2010

library(fixest)
library(haven)
library(tidyverse)
library(coefplot)
library(ggpubr)

## Load data
vote_08_10 = read_stata("vote_08_10.dta")

## Figure A1 ##
earmarks_num_pred = ggplot(data = vote_08_10, aes(x = log_area, y = solo_other_pres_earmarks_log_num)) + 
  geom_point(color = "blue", size = 1) +
  geom_smooth(method = "lm", se = T, color = "red") +
  labs(x = "log(Area)", y = "log(# Earmarks)") +
  stat_cor(method = "pearson", label.x = 3, label.y = .3)+
  theme_bw() 
ggsave(earmarks_num_pred, file = "earmarks_num_pred_bivariate.png", 
       width =4, height = 3.5, units = "in", dpi = 800)

summary(m1 <- feols(scale(solo_other_pres_earmarks_log_num) ~ scale(log_area) + scale(firstterm) + 
                      scale(abs(nokken_poole_dim1)) + 
                      scale(incvotep) + scale(incpres) +
                      scale(memberinpresparty) + scale(memberinhouseparty)
                    + scale(cmte_approp) + scale(cmte_ways) + scale(chair) + scale(rankingminority) + 
                      scale(partyleader) 
                    + scale(seniority) + scale(lesclassic) + scale(pct_urban) + scale(pct_college) + scale(pct_black) + scale(pct_hispanic) +
                      scale(log_median_income) | year, data = vote_08_10,
                    cluster = ~ state^district))


vars = c("log(Area)", "Freshman", "Ideological Extremity",
         "Incumbent Vote Share", "District Partisanship", "President's Party", "Democrat",
         "Appropriations", "Ways & Means", 
         "Committee Chair", "Ranking Minority", "Party Leader", "Seniority", "Legislative Effectiveness",
         "% Urban", "% College", "% Black", "% Hispanic",
         "log(Median Income)")
coefs = c(0.162899, -0.022015, -0.405303, -0.090684, 0.282773,
          0.016816, 0.285957, 0.315483, -0.051051, -0.048244, 
          -0.002139, -0.018819, 0.148143, 0.039046, 0.146106,
          -0.072325, -0.038736, -0.072840, 0.013628)
se = c(0.065762, 0.042029, 0.050660, 0.060504, 0.072399, 0.031440,
       0.044674, 0.028877, 0.044293, 0.031290, 0.042335, 0.081727,
       0.049280, 0.032963, 0.070891, 0.065558, 0.052162, 0.042323,
       0.058489)


plot_data =
  bind_cols(vars = vars, coefs = coefs, 
            se = se) %>%
  mutate(vars = factor(vars, levels = c("log(Median Income)", "% Hispanic",
                                        "% Black", "% College", "% Urban", 
                                        "Legislative Effectiveness", "Seniority", "Party Leader", "Ranking Minority",
                                        "Committee Chair", "Ways & Means", "Appropriations", "House Majority",
                                        "President's Party", "District Partisanship",
                                        "Incumbent Vote Share", "Ideological Extremity",
                                        "Freshman",
                                        "log(Area)")))


earmark_preds = ggplot(plot_data, aes(x = coefs,
                                      y = factor(vars),
                                      xmin = coefs - 1.96*se,
                                      xmax = coefs + 1.96*se,
                                      color = "blue")) + 
  geom_vline(xintercept = 0.0, linetype="dashed", 
             color = "gray", size=1)+
  geom_pointrange(size =0.5, position = position_dodge(width =.5)) + 
  scale_color_manual(values = c("blue")) +
  labs(x = "Coefficient", y = NULL) +
  scale_y_discrete(labels= c("log(Median Income)", "% Hispanic",
                                        "% Black", "% College", "% Urban", 
                                        "Legislative Effectiveness", "Seniority", "Party Leader", "Ranking Minority",
                                        "Committee Chair", "Ways & Means", "Appropriations", "House Majority",
                                        "President's Party", "District Partisanship",
                                        "Incumbent Vote Share", "Ideological Extremity",
                                        "Freshman",
                                        "log(Area)")) +
  theme_bw() +
  theme(legend.position="none")
ggsave(earmark_preds, file = "earmark_num_preds.png", 
       width =4, height = 3.5, units = "in", dpi = 800)


## Figure A2 ##

earmarks_usd_pred = ggplot(data = vote_08_10, aes(x = log_area, y = solo_other_pres_earmarks_log_usd)) + 
  geom_point(color = "blue", size = 1) +
  geom_smooth(method = "lm", se = T, color = "red") +
  labs(x = "log(Area)", y = "log(Earmarks USD Amount)") +
  stat_cor(method = "pearson", label.x = 3, label.y = 2.5)+
  theme_bw() 
ggsave(earmarks_usd_pred, file = "earmarks_usd_pred_bivariate.png", 
       width =4, height = 3.5, units = "in", dpi = 800)

summary(m1 <- feols(scale(solo_other_pres_earmarks_log_usd) ~ scale(log_area) + scale(firstterm) + 
                      scale(abs(nokken_poole_dim1)) + 
                      scale(incvotep) + scale(incpres) +
                      scale(memberinpresparty) + scale(memberinhouseparty)
                    + scale(cmte_approp) + scale(cmte_ways) + scale(chair) + scale(rankingminority) + 
                      scale(partyleader) 
                    + scale(seniority) + scale(lesclassic) + scale(pct_urban) + scale(pct_college) + scale(pct_black) + scale(pct_hispanic) +
                      scale(log_median_income) | year, data = vote_08_10,
                    cluster = ~ state^district))

vars = c("log(Area)", "Freshman", "Ideological Extremity",
         "Incumbent Vote Share", "District Partisanship", "President's Party", "Democrat",
         "Appropriations", "Ways & Means", 
         "Committee Chair", "Ranking Minority", "Party Leader", "Seniority", "Legislative Effectiveness",
         "% Urban", "% College", "% Black", "% Hispanic",
         "log(Median Income)")
coefs = c(0.115693, -0.020312, -0.379753, -0.057557, 0.256137,
          0.102637, 0.184679, 0.107864, -0.049924, -0.017979, 
          -0.026490, -0.092417, 0.144683, -0.010894, 0.149636,
          -0.048136, -0.037200, -0.030480, -0.013317)
se = c(0.063004, 0.048891, 0.059504, 0.063500, 0.072855,
       0.037820, 0.042528, 0.023593, 0.048453, 0.025682,
       0.046749, 0.088854, 0.049996, 0.031410, 0.073281,
       0.072702, 0.049919, 0.041818, 0.057933)

plot_data =
  bind_cols(vars = vars, coefs = coefs, 
            se = se) %>%
  mutate(vars = factor(vars, levels = c("log(Median Income)", "% Hispanic",
                                        "% Black", "% College", "% Urban", 
                                        "Legislative Effectiveness", "Seniority", "Party Leader", "Ranking Minority",
                                        "Committee Chair", "Ways & Means", "Appropriations", "House Majority",
                                        "President's Party", "District Partisanship",
                                        "Incumbent Vote Share", "Ideological Extremity",
                                        "Freshman",
                                        "log(Area)")))

earmark_preds = ggplot(plot_data, aes(x = coefs,
                                      y = factor(vars),
                                      xmin = coefs - 1.96*se,
                                      xmax = coefs + 1.96*se,
                                      color = "blue")) + 
  geom_vline(xintercept = 0.0, linetype="dashed", 
             color = "gray", size=1)+
  geom_pointrange(size =0.5, position = position_dodge(width =.5)) + 
  scale_color_manual(values = c("blue")) +
  labs(x = "Coefficient", y = NULL) +
  scale_y_discrete(labels= c("log(Median Income)", "% Hispanic",
                             "% Black", "% College", "% Urban", 
                             "Legislative Effectiveness", "Seniority", "Party Leader", "Ranking Minority",
                             "Committee Chair", "Ways & Means", "Appropriations", "House Majority",
                             "President's Party", "District Partisanship",
                             "Incumbent Vote Share", "Ideological Extremity",
                             "Freshman",
                             "log(Area)")) +
  theme_bw() +
  theme(legend.position="none")
ggsave(earmark_preds, file = "earmark_usd_preds.png", 
       width =4, height = 3.5, units = "in", dpi = 800)

## Table 1 ##
summary(m1 <- feols(incvote ~ solo_other_pres_earmarks_log_num + 
                      po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep | year^dem, 
                    data = vote_08_10, 
                    cluster = ~ state^district))
summary(m2 <- feols(incvote ~ solo_other_pres_earmarks_log_num*log_area + 
                po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep | year^dem, 
              data = vote_08_10, 
              cluster = ~ state^district))

## Table A1 ##
summary(m1_dollar <- feols(incvote ~ solo_other_pres_earmarks_log_usd + 
                      po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep | year^dem, 
                    data = vote_08_10, 
                    cluster = ~ state^district))
summary(m2_dollar <- feols(incvote ~ solo_other_pres_earmarks_log_usd*log_area + 
                             po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep | year^dem, 
                    data = vote_08_10, 
                    cluster = ~ state^district))

## Table A2 ##
summary(m1 <- feols(incvote ~ solo_other_pres_earmarks_num + 
                      po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep | year^dem, 
                    data = vote_08_10, 
                    cluster = ~ state^district))
summary(m2 <- feols(incvote ~ solo_other_pres_earmarks_num*log_area + 
                      po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep | year^dem, 
                    data = vote_08_10, 
                    cluster = ~ state^district))

## Table A3 ##
summary(m1 <- feols(incvote ~ solo_other_pres_earmarks_log_num + 
                      po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep 
                    + mrp_ideology | year^dem, 
                    data = vote_08_10, 
                    cluster = ~ state^district))
summary(m2 <- feols(incvote ~ solo_other_pres_earmarks_log_num*log_area + 
                      po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep + mrp_ideology | year^dem, 
                    data = vote_08_10, 
                    cluster = ~ state^district))

## Table A4 ##
summary(m1 <- feols(incvote ~ solo_other_pres_earmarks_log_num + 
                      fr  + abs(nokken_poole_dim1) + incpres + incvotep | year^dem, 
                    data = vote_08_10, 
                    cluster = ~ state^district))
summary(m2 <- feols(incvote ~ solo_other_pres_earmarks_log_num*log_area + 
                      fr  + abs(nokken_poole_dim1) + incpres + incvotep | year^dem, 
                    data = vote_08_10, 
                    cluster = ~ state^district))

## Table A5 ##
summary(m1_dem <- feols(incvote ~ solo_other_pres_earmarks_log_num + 
                          po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep 
                        | year, 
                        data = vote_08_10 %>% filter(dem == 1), 
                        cluster = ~ state^district))
summary(m4_rep <- feols(incvote ~ solo_other_pres_earmarks_log_num + 
                          po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep 
                        | year, 
                        data = vote_08_10 %>% filter(dem == 0), 
                        cluster = ~ state^district))
summary(m2_dem <- feols(incvote ~ solo_other_pres_earmarks_log_num*log_area + 
                          po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep 
                        | year, 
                        data = vote_08_10 %>% filter(dem == 1), 
                        cluster = ~ state^district))
summary(m5_rep <- feols(incvote ~ solo_other_pres_earmarks_log_num*log_area + 
                          po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep 
                        | year, 
                        data = vote_08_10 %>% filter(dem == 0), 
                        cluster = ~ state^district))

## Table A6 ##
summary(m1_fx2008 <- feols(incvote ~ solo_other_pres_earmarks_log_num + 
                             po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep | dem, 
                           data = vote_08_10 %>% filter(year == 2008), 
                           cluster = ~ state^district))
summary(m3_fx2010 <- feols(incvote ~ solo_other_pres_earmarks_log_num + 
                             po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep | dem, 
                           data = vote_08_10 %>% filter(year == 2010), 
                           cluster = ~ state^district))
summary(m2_fx2008 <- feols(incvote ~ solo_other_pres_earmarks_log_num*log_area + 
                             po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep | dem, 
                           data = vote_08_10 %>% filter(year == 2008), 
                           cluster = ~ state^district))
summary(m4_fx2010 <- feols(incvote ~ solo_other_pres_earmarks_log_num*log_area + 
                             po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep | dem, 
                           data = vote_08_10 %>% filter(year == 2010), 
                           cluster = ~ state^district))

## Table A7 ##
summary(m1_region <- feols(incvote ~ solo_other_pres_earmarks_log_num + 
                             po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep | region + year^dem, 
                           data = vote_08_10, 
                           cluster = ~ state^district))
summary(m2_region <- feols(incvote ~ solo_other_pres_earmarks_log_num*log_area + 
                             po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep | region + year^dem, 
                           data = vote_08_10, 
                           cluster = ~ state^district))
summary(m4_state <- feols(incvote ~ solo_other_pres_earmarks_log_num + 
                            po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep | state + year^dem, 
                          data = vote_08_10, 
                          cluster = ~ state^district))
summary(m5_state <- feols(incvote ~ solo_other_pres_earmarks_log_num*log_area + 
                            po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep | state + year^dem, 
                          data = vote_08_10, 
                          cluster = ~ state^district))

## Table A8 ##
summary(m1_institutional <- feols(incvote ~ solo_other_pres_earmarks_log_num + 
                                    po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep 
                                  + cmte_approp + cmte_ways + chair + rankingminority + partyleader 
                                  + seniority + lesclassic | year^dem, 
                                  data = vote_08_10,
                                  cluster = ~ state^district))
summary(m2_institutional <- feols(incvote ~ solo_other_pres_earmarks_log_num*log_area + 
                                    po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep 
                                  + cmte_approp + cmte_ways + chair + rankingminority + partyleader 
                                  + seniority + lesclassic | year^dem, 
                                  data = vote_08_10,
                                  cluster = ~ state^district))

## Table A9 ##
summary(m1_demos <- feols(incvote ~ solo_other_pres_earmarks_log_num + 
                            po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep +
                            + pct_urban + pct_college + pct_black + pct_hispanic +
                            log_median_income + hhi + one_county | year^dem, 
                          data = vote_08_10, 
                          cluster = ~ state^district))
summary(m2_demos <- feols(incvote ~ solo_other_pres_earmarks_log_num*log_area + 
                            po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep +
                            + pct_urban + pct_college + pct_black + pct_hispanic +
                            log_median_income  + hhi + one_county  | year^dem, 
                          data = vote_08_10, 
                          cluster = ~ state^district))

## Table A10 ##
summary(m1 <- feols(incvote ~ solo_other_pres_earmarks_log_num + 
                      po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep  | state^district + year^dem, 
                    data = vote_08_10, 
                    cluster = ~ state^district))
summary(m2 <- feols(incvote ~ solo_other_pres_earmarks_log_num + 
                      solo_other_pres_earmarks_log_num:log_area + 
                      po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep | state^district + year^dem, 
                    data = vote_08_10, 
                    cluster = ~ state^district))

## Table A11 ##
summary(m1 <- feols(incvote ~ solo_other_pres_earmarks_log_num + 
                            po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep + pct_credit_claim + cov2dc
                          + newspaper + tv + radio + blog 
                          + know_house_majority | year^dem, 
                          data = vote_08_10, 
                          cluster = ~ state^district))
summary(m2 <- feols(incvote ~ solo_other_pres_earmarks_log_num*log_area + 
                            po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep + pct_credit_claim + cov2dc +
                            newspaper + tv + radio + blog 
                          + know_house_majority | year^dem, 
                          data = vote_08_10, 
                          cluster = ~ state^district))
summary(m3 <- feols(incvote ~ solo_other_pres_earmarks_log_num + 
                            po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep + pct_credit_claim + cov2dc
                          + newspaper + tv + radio + blog +
                            know_house_majority + contact | year^dem, 
                          data = vote_08_10, 
                          cluster = ~ state^district))
summary(m4 <- feols(incvote ~ solo_other_pres_earmarks_log_num*log_area + 
                            po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep + pct_credit_claim + cov2dc +
                            newspaper + tv + radio + blog +
                            know_house_majority + contact | year^dem, 
                          data = vote_08_10, 
                          cluster = ~ state^district))
summary(m5 <- feols(incvote ~ solo_other_pres_earmarks_log_num + 
                            po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep + pct_credit_claim + cov2dc  
                          + newspaper + tv + radio + blog +
                            know_house_majority + storycount10 | year^dem, 
                          data = vote_08_10, 
                          cluster = ~ state^district))
summary(m6 <- feols(incvote ~ solo_other_pres_earmarks_log_num*log_area + 
                            po1 + spendgap + fr  + abs(nokken_poole_dim1) + incpres + incvotep + pct_credit_claim + cov2dc  +
                            newspaper + tv + radio + blog +
                            know_house_majority + storycount10 | year^dem, 
                          data = vote_08_10, 
                          cluster = ~ state^district))
